package com.whxd.system.resource.ctrl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.whxd.framework.common.dto.TreeDto;
import com.whxd.framework.security.SecurityUtils;
import com.whxd.system.resource.entity.Resource;
import com.whxd.system.resource.service.ResourceService;

/**
 * 资源控制类
 * 
 * @author 张世鹏
 * @since 2015-2-28
 */
@Controller
@RequestMapping("/system/resource")
public class ResourceCtrl {

	private static Logger logger = LoggerFactory.getLogger(ResourceCtrl.class);

	@Autowired
	private ResourceService resourceService;

	/**
	 * 查询我的菜单
	 * 
	 * @author 张世鹏
	 * @since 2015-2-28
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	@RequestMapping("/findMyTree")
	@ResponseBody
	public List<TreeDto> findAllTree() throws JsonParseException,
			JsonMappingException, IOException {
		logger.info("查询树形菜单");

		String currentUsername = SecurityUtils.getCurrentSecurityUser()
				.getUsername();
		List<Resource> resources = resourceService.findMyMenu(currentUsername);
		
		List<TreeDto> dtos=new ArrayList<TreeDto>();
		
		for (Resource resource : resources) {
			TreeDto dto=new TreeDto();
			dto.setId(resource.getId());
			dto.setPname(resource.getName());
			dto.setText(resource.getDescription());
			dto.setAttributes(resource.getString());
			dtos.add(dto);
		}
		return dtos;
	}
}
